package com.ls.cloud.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * @author: candy33
 * @Date: 2022/05/04 13:14
 * com.ls.cloud.common
 */
@NoArgsConstructor
@Data
public class ResultData<T> implements Serializable {

    /**
     * 结果编码(500:失败, 200: 成功, 204: 警告)
     */
    private int code;

    /**
     * 信息描述
     */
    private String message = "";

    /**
     * 数据
     */
    private T data;

    /**
     * 时间戳
     */
    private Long timestamp = System.currentTimeMillis();

    /**
     * 是否成功
     */
//    @JsonIgnoreProperties(ignoreUnknown = true)
    private boolean success;

    public static final int CODE_FAIL = 500;
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_WARNING = 204;

    public static final String TEXT_FAIL = "操作失败";
    public static final String TEXT_SUCCESS = "操作成功";

    public ResultData(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultData(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public void setCode(int code, String message) {
        this.setCode(code, message, null);
    }

    public void setCode(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return (this.code == CODE_SUCCESS);
    }

    public static <T> boolean isSuccess(ResultData<T> ResultData) {
        return (ResultData != null && ResultData.getCode() == CODE_SUCCESS);
    }

    public static <T> ResultData<T> fail() {
        return new ResultData<>(CODE_FAIL, TEXT_FAIL);
    }

    public static <T> ResultData<T> fail(String message) {
        return set(CODE_FAIL, message, null);
    }

    public static <T> ResultData<T> fail(String message, T data) {
        return set(CODE_FAIL, message, data);
    }

    public static <T> ResultData<T> success() {
        return new ResultData<>(CODE_SUCCESS, TEXT_SUCCESS);
    }

    public ResultData ok(String message) {
        return success(message, null);
    }

    public static <T> ResultData<T> success(String message, T data) {
        return set(CODE_SUCCESS, message, data);
    }

    public static <T> ResultData<T> success(T data) {
        return set(CODE_SUCCESS, TEXT_SUCCESS, data);
    }

    public static <T> ResultData<T> warning(String message) {
        return set(CODE_WARNING, message, null);
    }

    public static <T> ResultData<T> warning(String message, T data) {
        return new ResultData<>(CODE_WARNING, message, data);
    }

    public static <T> ResultData<T> set(int code, String message, T data) {
        return new ResultData<>(code, message, data);
    }

//    @Override
//    public String toString() {
//        return JsonUtil.toString(this);
//    }
}